package com.hjm.common.utils;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.fastdfs.FastDFSClient;
import com.hjm.common.utils.security.ShiroUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName FileOrImgUtils
 * @Deacription 上传文件或者图片工具类
 * @Version 1.0
 **/
public class FileOrImgUtils {



    /**
     * @Description 上传图片
     * @Param [imgFile]
     * @return java.lang.String
     **/
    public static String uploadImg(MultipartFile imgFile) throws Exception {
        String clientName = imgFile.getOriginalFilename();
        if (StringUtils.isEmpty(clientName)) {
            return null;
        }
        String fileName = null;
        fileName = clientName.substring(clientName.indexOf("."), clientName.length());
        //临时文件
        File dfile = Utils.fileCopyNew(imgFile);
        //上传至Fast的图片数据
        Map<String, String> metaList = new HashMap<String, String>();
        metaList.put("fileName", fileName);
        metaList.put("fileType", imgFile.getName());
        metaList.put("author", ShiroUtils.getSysUser().getLoginName());
        metaList.put("date", Utils.formateDate(2));//年月日 时分秒
        String fid = FastDFSClient.uploadFile(dfile, dfile.getName(), metaList);
        if (StringUtils.isEmpty(fid)) {
            throw  new BusinessException("图片上传失败,请重新上传");
        }
        dfile.delete();
        return fid;
    }

    /**
     * @Description 上传图片
     * @Param [imgFile]
     * @return java.lang.String
     **/
    public static String uploadImgApk(MultipartFile imgFile) throws Exception {
        String clientName = imgFile.getOriginalFilename();
        if (StringUtils.isEmpty(clientName)) {
            return null;
        }
        String fileName = null;
        fileName = clientName.substring(clientName.indexOf("."), clientName.length());
        //临时文件
        File dfile = Utils.fileCopyNew(imgFile);
        //上传至Fast的图片数据
        Map<String, String> metaList = new HashMap<String, String>();
        metaList.put("fileName", fileName);
        metaList.put("fileType", imgFile.getName());
        metaList.put("author", ShiroUtils.getSysUser().getLoginName());
        metaList.put("date", Utils.formateDate(2));//年月日 时分秒
        String fid = FastDFSClient.uploadFile(dfile, dfile.getName(), metaList);
        if (StringUtils.isEmpty(fid)) {
            throw  new BusinessException("图片上传失败,请重新上传");
        }
        dfile.delete();
        return fid;
    }



}
